package com.ctz.commonframe.utils;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.FileProvider;

import com.ctz.commonframe.ui.BaseActivity;

public class ApplicationUtil {
	/**
	 * 应用是否已安装
	 * 
	 * @param context
	 * @param packageName
	 *            应用的包名
	 * @return
	 */
	public static boolean isInstalled(Context context, String packageName) {
		PackageManager manager = context.getPackageManager();
		List<PackageInfo> infos = manager.getInstalledPackages(0);
		for (PackageInfo info : infos) {
			if (info.packageName.equalsIgnoreCase(packageName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取应用版本
	 * @param context
	 * @return
	 */
	public static String getAppVersion(Context context) {
		PackageManager manager = context.getPackageManager();
		try {
			PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
			if (info != null)
				return info.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前应用程序的版本号
	 */
	public static int getAppVersionCode(Context context) {
		int version = 0;
		try {
			version = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return version;
	}
	
	/**
	 * 安装应用
	 * 
	 * @param context
	 * @param path apk路径
	 */
	public static void installApk(Context context, String path) {
		try {
			Intent intent = new Intent(Intent.ACTION_VIEW);
			File file = new File(path);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
				Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
				intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
				intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
			} else {
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				Uri uri = Uri.fromFile(file);
				intent.setDataAndType(uri, "application/vnd.android.package-archive");
			}
			context.startActivity(intent);
		} catch (Exception e) {
			((BaseActivity)context).showLongToast(e.getLocalizedMessage());
		}
	}

	/**
	 * 卸载应用
	 * 
	 * @param context
	 * @param packageName 应用包名
	 */
	public static void uninstallApk(Context context, String packageName) {
		if (isInstalled(context, packageName)) {
			Uri uri = Uri.parse("package:" + packageName);
			Intent intent = new Intent(Intent.ACTION_DELETE, uri);
			context.startActivity(intent);
		}
	}

	/**
	 * 打开应用
	 * 
	 * @param context
	 * @param packageName 应用包名
	 */
	public static void openApp(Context context, String packageName, Bundle data) {
		Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
		if (intent != null) {
			intent.putExtras(data);
			context.startActivity(intent);
		}
	}
}
